﻿// Author:					Le Xuan Manh
// Created:					2011-7-18
// Last Modified:			2011-7-18

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Hutech.Authentication.Library.Data;

namespace Hutech.Authentication.Library.Business
{
    public class Site
    {

        #region Constructors
        public Site()
        {
        }


        public Site(
        Guid siteId)
        {
            GetSite(
            siteId);
        }

        #endregion

        #region Private Properties

        private Guid siteId = Guid.Empty;
        private string name = string.Empty;
        private string descriptions = string.Empty;
        private Guid parentId = Guid.Empty;

        #endregion

        #region Public Properties

        public Guid SiteId
        {
            get
            {
                return siteId;
            }
            set
            {
                siteId = value;
            }
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public string Descriptions
        {
            get
            {
                return descriptions;
            }
            set
            {
                descriptions = value;
            }
        }
        public Guid ParentId
        {
            get
            {
                return parentId;
            }
            set
            {
                parentId = value;
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Gets an instance of Site.
        /// </summary>
        /// <param name="siteId"> siteId </param>
        private void GetSite(
        Guid siteId)
        {
            using (IDataReader reader = DBSite.GetOne(
            siteId))
            {
                PopulateFromReader(reader);
            }
        }


        private void PopulateFromReader(IDataReader reader)
        {
            if (reader.Read())
            {
                this.siteId = new Guid(reader["SiteId"].ToString());
                this.name = reader["Name"].ToString();
                this.descriptions = reader["Descriptions"].ToString();
                this.parentId = new Guid(reader["ParentId"].ToString());
            }
        }

        /// <summary>
        /// Persists a new instance of Site. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private bool Create()
        {
            this.siteId = Guid.NewGuid();

            int rowsAffected = DBSite.Create(
            this.siteId,
            this.name,
            this.descriptions,
            this.parentId);

            return (rowsAffected > 0);
        }


        /// <summary>
        /// Updates this instance of Site. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private bool Update()
        {
            return DBSite.Update(
            this.siteId,
            this.name,
            this.descriptions,
            this.parentId);
        }





        #endregion

        #region Public Methods

        /// <summary>
        /// Saves this instance of Site. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        public bool Save()
        {
            if (this.siteId != Guid.Empty)
            {
                return Update();
            }
            else
            {
                return Create();
            }
        }




        #endregion

        #region Static Methods

        /// <summary>
        /// Deletes an instance of Site. Returns true on success.
        /// </summary>
        /// <param name="siteId"> siteId </param>
        /// <returns>bool</returns>
        public static bool Delete(
        Guid siteId)
        {
            return DBSite.Delete(
            siteId);
        }


        /// <summary>
        /// Gets a count of Site. 
        /// </summary>
        public static int GetCount()
        {
            return DBSite.GetCount();
        }

        private static List<Site> LoadListFromReader(IDataReader reader)
        {
            List<Site> siteList = new List<Site>();
            try
            {
                while (reader.Read())
                {
                    Site site = new Site();
                    site.siteId = new Guid(reader["SiteId"].ToString());
                    site.name = reader["Name"].ToString();
                    site.descriptions = reader["Descriptions"].ToString();
                    site.parentId = new Guid(reader["ParentId"].ToString());
                    siteList.Add(site);
                }
            }
            finally
            {
                reader.Close();
            }

            return siteList;
        }

        /// <summary>
        /// Gets an IList with all instances of Site.
        /// </summary>
        public static List<Site> GetAll()
        {
            IDataReader reader = DBSite.GetAll();
            return LoadListFromReader(reader);
        }

        /// <summary>
        /// Gets an IList with page of instances of Site.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static List<Site> GetPage(int pageNumber, int pageSize, out int totalPages)
        {
            totalPages = 1;
            return LoadListFromReader(DBSite.GetPage(pageNumber, pageSize, out totalPages));
        }



        #endregion
    }
}
